package SimpleCalculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleCalculator extends JFrame implements ActionListener {
    private JTextField display;
    private JPanel panel;
    private String[] buttons = {
            "7", "8", "9", "/",
            "4", "5", "6", "*",
            "1", "2", "3", "-",
            "0", "C", "=", "+"};

    private Double firstNumber = null;
    private Character operator = null;

    public SimpleCalculator() {
        setTitle("Simple Calculator");
        setSize(300, 400);
        setLayout(null);

        display = new JTextField();
        display.setBounds(30, 30, 220, 40);
        display.setEditable(false);
        display.setHorizontalAlignment(JTextField.RIGHT);// 确保最新的数值总是显示在文本字段的最右侧

        panel = new JPanel();
        panel.setBounds(30, 80, 220, 240);
        panel.setLayout(new GridLayout(4, 4, 10, 10));

        for (String text : buttons) {
            JButton button = new JButton(text);
            button.addActionListener(this);
            panel.add(button);
        }

        add(display);
        add(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        if ("0123456789".indexOf(command) >= 0) {
            display.setText(display.getText() + command);
        } else if ("+-*/".indexOf(command) >= 0) { // 不支持连续操作
            if (firstNumber == null) {
                firstNumber = Double.parseDouble(display.getText());
                display.setText("");
            }
            if (operator == null) {
                operator = command.charAt(0);// command中就是操作符，按钮的标签文本
                display.setText("");
            }
        } else if ("C".equals(command)) {
            display.setText("");
            firstNumber = null;
            operator = null;
        } else if ("=".equals(command)) {
            if (firstNumber != null && operator != null) {
                double secondNumber = Double.parseDouble(display.getText());
                double result = calculate(firstNumber, secondNumber, operator);
                display.setText(String.valueOf(result));
                firstNumber = null;
                operator = null;
            } else {
                JOptionPane.showMessageDialog(this, "Error! Incomplete calculation.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private double calculate(double num1, double num2, Character op) {
        double result = 0;
        switch (op) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                if (num2 != 0)
                    result = num1 / num2;
                else
                    JOptionPane.showMessageDialog(this,
                            "Error! Division by zero.", "Error", JOptionPane.ERROR_MESSAGE);
                break;
        }
        return result;
    }

    public static void main(String[] args) {
        new SimpleCalculator();
    }
}